var countNodes = function(root) {
    let level = 0
    let node = root
    while(node.left!==null){
        level++
        node = node.left
    }
    let left = 1 <<level
    let right = (1 <<(level+1)) - 1
    
    while(left<right){
        let mid = Math.floor((right-left+1)/2) + left
        if(exists(node,level,mid)){
             left = mid
        }else{
            right = mid -1
        }
    }
    
};
const exists =function(root,level,mid){
    let bits = 1<<(level-1)
    let node = root
    while(node&&bits>0){
        if(bits & mid){
            node = node.right
        }else{
            node = node .left
        }
        bits >>= 1;
    }
    return node!==null
}